package com.linyaonan.zuochengyun._01._03;

import javax.print.attribute.standard.Fidelity;

/**
 *
 * 返回一个数组中任意一个局部最小数索引
 *
 * @author: Lin
 * @date: 2022/7/24
 */
public class LocalMinimum {

    public static void main(String[] args) {
        int [] org = {2,1,3,4,5,6,7,8};
        int i = find(org);
        System.out.println(i);
    }

    public static int find(int[] org) {
        if (org == null || org.length == 0) {
            return -1;
        }

        if (org[0] < org[1]) {
            return 0;
        }

        if (org[org.length - 1] < org[org.length - 2]) {
            return org.length - 1;
        }

        if (org.length == 3) {
            return -1;
        }

        int l = 0;
        int r = org.length - 1;

        while (l < r) {
            int mid = l + ((r - l) >> 1);

            if (org[mid] < org[mid + 1] && org[mid] < org[mid - 1]) {
                return mid;
            } else {
                if (org[mid - 1] < org[mid]) {
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            }
        }

        return -1;
    }

}
